--
-- All the utils for AspireML so it doesn't clutter up the main addon
-- 

function AspireML:Whisper( player, message )	
	if player == self.PLAYER_NAME then return end
	
	self.IGNOREWHISPERS[message] = time();
	SendChatMessage(message, "WHISPER", nil, player)
end

-- Filter outgoing whispers
function AspireML_ChatFilterOutgoing( self, event, message, unitname )

	if not message or not unitname then return false end
	if not AspireML.IGNOREWHISPERS[message] then return false end
		
	local now = time()		
	-- remove all hidings that are over 20s old
	for key,hide in pairs(AspireML.IGNOREWHISPERS) do		
		if ( now-hide > 20 ) then
			AspireML.IGNOREWHISPERS[key] = nil
		end
	end
	return true
end

-- Filter incoming whispers
function AspireML_ChatFilterIncoming( self, event, message, unitname )

	-- only chat whispers
	if event ~= "CHAT_MSG_WHISPER" then return false end
	
	-- only from units in raid
	if not UnitInRaid( unitname ) then return false end

	-- only when bidding is enabled
	if AspireML.mode ~= "bid" then return false end	
	
	-- parse whisper for item
	local item = AspireML:ParseItem(message, true)

	-- no item in link and no "offspec"
	if not item and not string.find(message:lower(), "offspec") then return false end
		
	return true
end

ChatFrame_AddMessageEventFilter("CHAT_MSG_WHISPER_INFORM", AspireML_ChatFilterOutgoing)
ChatFrame_AddMessageEventFilter("CHAT_MSG_WHISPER", AspireML_ChatFilterIncoming)


function AspireML:ParseItem(s, silent)
	local item
	local itemid = string.gsub(s,".-\124H([^\124]*)\124h.*", "%1");
	local itemstring = itemid
	itemid = self:Explode(":", itemid)
	-- Check if we found an item. No more faggots linking spells thinking they are super funny
	itemid = (#itemid > 0 and tostring(itemid[1]) == "item") and itemid[2] or nil	
	-- Then get info from item id (NOTE: will return nil if item is not in local cache)
	-- We want to do it like this so we can return the actual Item ID
	if (itemid) then
		item = {}
		item.name, item.link, item.rarity, item.level, _, item.type, item.subtype, _, item.slot, item.texture = GetItemInfo(itemid);
		
		-- force load of the item data if it wasn't in cache
		if not item.link then 
			GameTooltip:SetHyperlink( itemstring )
			return -1
		end
		
		item.string = s -- Ehh yeah, for looting... fml
		item.itemid = itemid
		item.istoken = (item.subtype == "Junk") and true or nil -- This check works on t10 tokens, not sure if all tokens react like this
		item.isweapon = (item.type == "Weapon" and item.subtype ~= "Wands") and true or nil -- Checks if weapon. Wands don't count.
	end
	if not silent then assert(item, (("ERROR: Could not parse item: \"%s\". Please click item to make sure it is in your local cache and try again."):format(s))) end
	
	return item or itemid;
end

function AspireML:Explode(d, str)
	local t, ll
	t={}
	ll=0
	if(#str == 1) then return str end
		while true do
			l=strfind(str, d, ll+1, true)
			if l~=nil then
				tinsert(t, strsub(str, ll, l-1))
				ll=l+1
			else
				tinsert(t, strsub(str,ll))
				break
			end
		end
	return t
end

function AspireML:SortRolls( a, b )	
	return tonumber( a.message ) > tonumber( b.message );
end

function AspireML:GetColoredName(unit)
	local class = select(2, UnitClass(unit)) or "WARRIOR"
	local color = RAID_CLASS_COLORS[class]
	return string.format("|cFF%02x%02x%02x%s|r", color.r*255, color.g*255, color.b*255, UnitName(unit) or "Unknown")
end